using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.UI.Base;
using Habanero.UI.Win;
using NUnit.Framework;
using UseCaseManager.BO;
using UseCaseManager.Test.BO;
using UseCaseManager.UI;

namespace UseCaseManager.Test.UI
{
    /// <summary>
    /// This is a test class for testing the <see cref="ReleaseDropPlanningControl"/>
    /// </summary>
    [TestFixture]
    public class TestReleaseDropPlanning
    {


        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //Executes once for all tests in this fixture
            //BORegistry.DataAccessor = new DataAccessorDB();

            ClassDef.ClassDefs.Clear();
            ClassDef.LoadClassDefs(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()));
        }

        [SetUp]
        public void TestSetup()
        {
            //Test Setup Runs prior to each test in this fixture executing.
            BORegistry.DataAccessor = new DataAccessorInMemory();
            TestUtils.ClearAllTables();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            TestUtils.WaitForGC();
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();
        }

        [Test]
        public void Test_CreateDropPlanning()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ReleaseDropPlanningControl dropPlanner = new ReleaseDropPlanningControl();
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanner);
            Assert.IsNotNull(dropPlanner.ReleaseSelector);
            Assert.IsNotNull(dropPlanner.DropPlanningControl);

        }

        [Test]
        public void Test_CreateReleaseDropPlanningControl_BorderLayoutManagerSetup()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            const int expectedOverallWidth = 223;
            const int expectedHeight = 355;
            const int expectedBorder = 0;
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl
                                                                        {
                                                                            Width = expectedOverallWidth,
                                                                            Height = expectedHeight
                                                                        };
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedBorder, releaseDropPlanningControl.ReleaseSelector.Left);
            const int expectedReleaseSelectorHeight = 150;
            Assert.AreEqual(expectedReleaseSelectorHeight, releaseDropPlanningControl.ReleaseSelector.Height);
            Assert.AreEqual(expectedOverallWidth - expectedBorder, releaseDropPlanningControl.ReleaseSelector.Width);
            Assert.AreEqual(expectedOverallWidth, releaseDropPlanningControl.DropPlanningControl.Width);
            Assert.AreEqual
                (expectedHeight - expectedReleaseSelectorHeight, releaseDropPlanningControl.DropPlanningControl.Height);
        }

        [Test]
        public void Test_ReleaseDropPlanningControl_ChangeSize_MultiSelectorChangesSize()
        {
            //---------------Set up test pack-------------------
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl
                                                                        {Width = 544, Height = 877};
            const int expectedWidth = 223;
            const int expectedHeight = 355;
            const int expectedReleaseSelectorHeight = 150;
            //---------------Assert Precondition----------------
            Assert.AreEqual(expectedReleaseSelectorHeight, releaseDropPlanningControl.ReleaseSelector.Height);
            Assert.AreEqual(releaseDropPlanningControl.Width, releaseDropPlanningControl.ReleaseSelector.Width);
            Assert.AreEqual(releaseDropPlanningControl.Width, releaseDropPlanningControl.DropPlanningControl.Width);
            Assert.AreEqual
                (releaseDropPlanningControl.Height - expectedReleaseSelectorHeight, releaseDropPlanningControl.DropPlanningControl.Height);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.Width = expectedWidth;
            releaseDropPlanningControl.Height = expectedHeight;
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedWidth, releaseDropPlanningControl.DropPlanningControl.Width);
            Assert.AreEqual
                (expectedHeight - expectedReleaseSelectorHeight, releaseDropPlanningControl.DropPlanningControl.Height);
        }

        [Test]
        public void Test_LoadsListOfAvailableUseCases()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<Release> releaseCol = Broker.GetBusinessObjectCollection<Release>("");
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, releaseCol.Count);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(releaseDropPlanningControl.ReleasesCol);
            Assert.AreEqual(0, releaseSelector.GridSelector.Grid.RowCount);
        }

        [Test]
        public void Test_LoadsListOfReleases()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            BusinessObjectCollection<Release> releaseCol = Broker.GetBusinessObjectCollection<Release>("");
            releaseCol.Add(release);
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, releaseCol.Count);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(releaseDropPlanningControl.ReleasesCol);
            Assert.AreEqual(1, releaseSelector.GridSelector.Grid.RowCount);
        }

        [Test]
        public void Test_LoadsListOfReleases_LoadsFirstRelease()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            BusinessObjectCollection<Release> releaseCol = Broker.GetBusinessObjectCollection<Release>("");
            releaseCol.Add(release);
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, releaseCol.Count);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(releaseDropPlanningControl.ReleasesCol);
            Assert.AreEqual(1, releaseSelector.GridSelector.Grid.RowCount);
            Assert.AreSame(release, releaseDropPlanningControl.ReleaseSelector.GridSelector.SelectedBusinessObject);
        }

        [Test]
        public void Test_LoadsListWithTwoReleases()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            BusinessObjectCollection<Release> releaseCol = Broker.GetBusinessObjectCollection<Release>("");
            releaseCol.Add(release);
            releaseCol.Add(TestUtilsRelease.CreateUnsavedValidRelease());
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, releaseCol.Count);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(releaseDropPlanningControl.ReleasesCol);
            Assert.AreEqual(2, releaseSelector.GridSelector.Grid.RowCount);
            Assert.AreSame
                (release, releaseDropPlanningControl.ReleaseSelector.GridSelector.SelectedBusinessObject,
                 "Should load first item");
        }

        [Test]
        public void Test_ResetDropsNullClearsGrid()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            BusinessObjectCollection<Release> releaseCol = Broker.GetBusinessObjectCollection<Release>("");
            releaseCol.Add(release);
            releaseCol.Add(TestUtilsRelease.CreateUnsavedValidRelease());
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, releaseCol.Count);
            Assert.AreEqual(2, releaseSelector.GridSelector.Grid.RowCount);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleasesCol = null;
            //---------------Test Result -----------------------
            Assert.AreEqual(0, releaseSelector.GridSelector.Grid.RowCount);
        }

        [Test]
        public void Test_LoadsListOfReleases_AndDrops()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            BusinessObjectCollection<Release> releaseCol = new BusinessObjectCollection<Release>();
            releaseCol.Add(release);
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;

            release.Drops.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            DropPlanningControl dropPlanningControl = releaseDropPlanningControl.DropPlanningControl;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, releaseCol.Count);
            Assert.AreEqual(0, releaseSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(0, dropPlanningControl.DropSelector.GridSelector.Grid.RowCount);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(releaseDropPlanningControl.ReleasesCol);
            Assert.AreEqual(1, releaseSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(1, dropPlanningControl.DropSelector.GridSelector.Grid.RowCount);
        }

        [Test]
        public void Test_LoadsListOfDrops_TwoReleases_AndDrops()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            BusinessObjectCollection<Release> releaseCol = Broker.GetBusinessObjectCollection<Release>("");
            releaseCol.Add(release);
            release.Drops.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            release.Drops.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            releaseCol.Add(TestUtilsRelease.CreateUnsavedValidRelease());
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;

            TestUtilsDrop.CreateSavedDrop();
            DropPlanningControl dropPlanningControl = releaseDropPlanningControl.DropPlanningControl;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, releaseCol.Count);
            Assert.AreEqual(0, releaseSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(0, dropPlanningControl.DropSelector.GridSelector.Grid.RowCount);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(releaseDropPlanningControl.ReleasesCol);
            Assert.AreEqual(2, releaseSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(2, dropPlanningControl.DropSelector.GridSelector.Grid.RowCount);
        }

        [Test]
        public void Test_LoadsListOfReleasees_TwoDrops_AndDrops_ResetSelectedRelease()
        {
            //---------------Set up test pack-------------------
            Release release = TestUtilsRelease.CreateUnsavedValidRelease();
            BusinessObjectCollection<Release> releaseCol = Broker.GetBusinessObjectCollection<Release>("");
            releaseCol.Add(release);
            release.Drops.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            release.Drops.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            Release release2 = TestUtilsRelease.CreateUnsavedValidRelease();
            releaseCol.Add(release2);
            ReleaseDropPlanningControl releaseDropPlanningControl = new ReleaseDropPlanningControl();
            ReleasesGridControl releaseSelector = releaseDropPlanningControl.ReleaseSelector;
            
            TestUtilsDrop.CreateSavedDrop();
            DropPlanningControl dropPlanningControl = releaseDropPlanningControl.DropPlanningControl;
            releaseDropPlanningControl.ReleasesCol = releaseCol;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, releaseCol.Count);
            Assert.AreEqual(2, release.Drops.Count);
            Assert.AreEqual(2, releaseSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(2, dropPlanningControl.DropSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(0, release2.Drops.Count);
            //---------------Execute Test ----------------------
            releaseDropPlanningControl.ReleaseSelector.GridSelector.SelectedBusinessObject = release2;
            //---------------Test Result -----------------------
            Assert.AreEqual(release2.Drops.Count, dropPlanningControl.DropSelector.GridSelector.Grid.RowCount);
        }
    }
}